/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @author ben
 */
public class controller_TuyenTau {
    
     
    public List<model_TuyenTau> LoadList() throws Exception{
    
        try{
            DataProvider conn = new DataProvider();
            conn.Open();
            List<model_TuyenTau> lst = new ArrayList<>();
            String query = "SELECT * FROM tuyen_tau";
            //Statement st = conn.createStatement();
            ResultSet rs = conn.ExecuteQuery(query);

            while(rs.next()){
                model_TuyenTau tt = new model_TuyenTau();
                tt.setMATUYEN(rs.getString("MaTuyen"));
                tt.setTENTUYEN(rs.getString("TenTuyen"));

                lst.add(tt);
            }
            rs.close();
            conn.Close();
            return lst;
        
        } catch (Exception ex){
            System.err.println(ex.getMessage());
        }
        return null;
    }
    
    public void Xoa_TuyenTau(String matuyen) {
        DataProvider conn = new DataProvider();
        conn.Open();
        String query = String.format("DELETE FROM tuyen_tau WHERE MaTuyen = '%s'", matuyen);
        conn.ExecuteUpdate(query);
    }
    
    public int Them_TuyenTau(model_TuyenTau tt) {
        DataProvider conn = new DataProvider();
        conn.Open();
        String query = String.format("INSERT INTO tuyen_tau VALUES ('%s','%s')", 
                tt.getMATUYEN(), 
                tt.getTENTUYEN());   
        return conn.ExecuteUpdate(query);
    } 
    
    public int CapNhat_TuyenTau(model_TuyenTau tt){
        DataProvider conn = new DataProvider();
        conn.Open();
        
        String query = String.format("UPDATE tuyen_tau SET TenTuyen = '%s' where MaTuyen = '%s'",
                tt.getTENTUYEN(),
                tt.getMATUYEN()); 
        
        return conn.ExecuteUpdate(query);
    }
    
    public model_TuyenTau Load_CapNhat_TuyenTau(String matuyen) throws SQLException{
        DataProvider conn = new DataProvider();
        conn.Open();
        
        String query = String.format("SELECT * FROM tuyen_tau WHERE MaTuyen = '%s'", matuyen);
        ResultSet rs = conn.ExecuteQuery(query);
        
        model_TuyenTau tt = new model_TuyenTau();
        
        while (rs.next()) {
            tt.setMATUYEN(rs.getString("MaTuyen"));
            tt.setTENTUYEN(rs.getString("TenTuyen"));
        }
        return tt;
    }
    
    public List<model_TuyenTau> TimKiem_TuyenTau(String matuyen) throws SQLException {

        DataProvider conn = new DataProvider();
        conn.Open();

        List<model_TuyenTau> lst = new ArrayList<>();
        String query = String.format("SELECT * FROM tuyen_tau WHERE MaTuyen = '%s'", matuyen);
        ResultSet rs = conn.ExecuteQuery(query);

        while (rs.next()) {
            model_TuyenTau tt = new model_TuyenTau();

            tt.setMATUYEN(rs.getString("MaTuyen"));
            tt.setTENTUYEN(rs.getString("TenTuyen"));

            lst.add(tt);
        }
        rs.close();
        conn.Close();
        return lst;
    }
    
}
